import axios from "axios";

const state = {
    userName: localStorage.userName,
    usertoken: localStorage.usertoken,
    userList: [],   //所有用户的列表
    user_id: localStorage.user_id,
    userLogList: [], //单个用户的所有登录记录
    userCollectionList: [],//用户收藏诗歌列表
    commentList: [] //用户评论列表
}

const mutations = {
    //获取用户列表
    CHANGE_USER_LIST(state, userList) {
        state.userList = userList
    },
    //获取用户登录记录
    CHANGE_USER_LOG_LIST(state, userLogList) {
        state.userLogList = userLogList
    },
    //获取用户收藏诗歌列表
    CHANGE_USER_COLLECTION_LIST(state, userCollectionList) {
        state.userCollectionList = userCollectionList
    },
    //获取评论列表
    CHANGE_COMMENT_LIST(state, commentList) {
        state.commentList = commentList;
    }
}

const actions = {

    // 添加用户
    addUser(state, addUserForm) {
        return axios.post("/addUser", addUserForm);
    },
    //获取用户列表
    getUserlist({ commit }, params) {
        axios.get('/getUserList', { params }).then(res => {
            commit('CHANGE_USER_LIST', res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },
    //获取用户登录记录
    getUserLogList({ commit }, params) {
        axios.get('/getUserLogList', { params }).then(res => {
            commit("CHANGE_USER_LOG_LIST", res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },
    //获取用户详细信息
    getUserInformation(state, _id) {
        return axios.get('/getUserInformation', {
            params: {
                _id
            }
        })
    },
    //修改用户详细信息
    putUserInformation(state, userForm) {
        return axios.put("/putUserInformation", userForm);
    },
    //添加收藏
    addCollection(state, collectionForm) {
        return axios.post("/addCollection", collectionForm)
    },
    //获取用户收藏的古诗
    getCollection({ commit }, params) {
        axios.get("/getCollection", { params }).then(res => {
            commit("CHANGE_USER_COLLECTION_LIST", res.getList);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        });
    },
    //取消收藏(删除收藏的诗歌)
    deleteCollection(state, _id) {
        return axios.delete("/deleteCollection", {
            params: {
                _id
            }
        });
    },
    //提交评论(增加评论)
    addCommentList(state, commentForm) {
        return axios.post("/addCommentList", commentForm);
    },
    //获取评论
    getCommentList({ commit }, _id) {
        axios.get("/getCommentList", {
            params: {
                _id
            }
        }).then(res => {
            commit("CHANGE_COMMENT_LIST", res.data);
            commit('CHANGE_PAGE_INDEX', res.pageIndex);
        })
    },
    //增加点赞数
    putCommentNumber(state, poetryFrom) {
        return axios.put("/putCommentNumber", poetryFrom)
    },
    //获取所有的诗词
    getAllPoetryList() {
        return axios.get("/getAllPoetryList")
    }
}

export default {
    state, mutations, actions
}